package com.cxs.xiaomi.web.controller;

import com.alibaba.fastjson.JSON;
import com.cxs.xiaomi.domain.WeiXinResult;
import com.cxs.xiaomi.service.OrderService;
import com.cxs.xiaomi.service.impl.OrderServiceImpl;
import com.cxs.xiaomi.utils.StringUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/*
 * @Author:cxs
 * @Date:2021/9/17  14:33
 * @Version 1.0
 * @Motto:放下杂念,只为迎接明天更好的自己
 * */

@WebServlet(name = "PaySucessServlet",urlPatterns = "/paySuccess")
public class PaySucessServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        String result = request.getParameter("result");//json字符串
        if(StringUtil.empty(result)){
            request.setAttribute("msg","支付失败" );
            request.getRequestDispatcher("/message.jsp").forward(request, response);
            return;
        }
        //把result转成java对象
        WeiXinResult weiXinResult = JSON.parseObject(result, WeiXinResult.class);
        if(weiXinResult.getResult().getResultCode().equalsIgnoreCase("success")){
            //修改订单状态
            OrderService orderService=new OrderServiceImpl();
            String oid=weiXinResult.getResult().getOutTradeNo();
            orderService.updateState(oid,"2");
            request.setAttribute("msg","支付成功,请商家及时发货~~~~~" );
        }else{
            //支付失败
            request.setAttribute("msg","支付失败" );
        }
        request.getRequestDispatcher("/message.jsp").forward(request, response);
        
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
